package com.sneakxy.mybatis.commons.repository.query.domain.criterions;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.apache.ibatis.scripting.xmltags.IfSqlNode;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.session.Configuration;

import com.sneakxy.mybatis.commons.dialect.Dialect;
import com.sneakxy.mybatis.commons.orm.mapping.MybatisColumn;

/**
 * @author 潜行的青衣
 */
public class InableCriterion extends AbstractCriterion {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4303100182765458668L;
	
	private boolean inable;
	/**
	 * @param domainClass
	 * @param property
	 * @param value
	 */
	public InableCriterion(Class<?> domainClass, String property, Iterable<?> value, boolean inable) {
		super(domainClass, property, value);
		this.inable = inable;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybatis.commons.repository.query.domain.Criterion#toSql(java.lang.String, java.lang.String)
	 */
	@Override
	public SqlNode toSql(Configuration config, String operator, String parameterName) {
		Dialect dialect = this.getDialect(config);
		List<MybatisColumn> columns = this.getColumns(config.getTypeHandlerRegistry(), parameterName);
		MybatisColumn column = columns.get(0);
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(operator).append(" ").append(getOpen());
		sqlBuilder.append(dialect.formatColumnName(column.getColumn())).append(" ").append(this.isInable() ? "IN":"NOT IN").append(" ");
		List<SqlNode> contents = new ArrayList<SqlNode>();
		contents.add(new StaticTextSqlNode(sqlBuilder.toString()));
		contents.add(new ForEachSqlNode(config, new StaticTextSqlNode("#{item}"), parameterName, false, "index", "item", "(", ")", ","));
		contents.add(new StaticTextSqlNode(StringUtils.join(getClose(), " ")));
		return new IfSqlNode(new MixedSqlNode(contents), new StringBuilder().append(parameterName).append(".size > 0").toString());
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.getValue())
			.append(this.getProperty())
			.append(this.getDomainClass())
			.append(this.inable).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof InableCriterion)) {
			return false;
		}
		InableCriterion criterion = (InableCriterion) obj;
		return new EqualsBuilder()
				.append(this.getValue(), criterion.getValue())
				.append(this.getProperty(), criterion.getProperty())
				.append(this.getDomainClass(), criterion.getDomainClass())
				.append(this.inable, criterion.inable).isEquals();
	}
	
	public boolean isInable() {
		return inable;
	}

}
